package io.unicorn.modules.waterfgs.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.unicorn.common.utils.PageUtils;
import io.unicorn.common.utils.R;
import io.unicorn.common.validator.ValidatorUtils;
import io.unicorn.modules.waterfgs.entity.DataErrFgsEntity;
import io.unicorn.modules.waterfgs.entity.DataErrFixedInfoFgsEntity;
import io.unicorn.modules.waterfgs.service.DataErrFgsService;
import io.unicorn.modules.waterfgs.service.DataErrFixedInfoFgsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;



/**
 * ${comments}
 *
 * @author LNCG
 * @email LNCG@USTL.EDU.CN
 * @date 2023-04-01 17:07:40
 */
@RestController
@RequestMapping("waterfgs/dataerr")
public class DataErrFgsController {
    @Autowired
    private DataErrFgsService dataErrService;

    @Autowired
    private DataErrFixedInfoFgsService dataErrFixedInfoService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("waterfgs:dataerr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = dataErrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{deviceId}")
    @RequiresPermissions("waterfgs:dataerr:info")
    public R info(@PathVariable("deviceId") String deviceId){
        DataErrFgsEntity dataErr = dataErrService.getById(deviceId);

        return R.ok().put("dataErr", dataErr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("waterfgs:dataerr:save")
    public R save(@RequestBody DataErrFgsEntity dataErr){
        dataErrService.save(dataErr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("waterfgs:dataerr:update")
    public R update(@RequestBody DataErrFgsEntity dataErr){
        ValidatorUtils.validateEntity(dataErr);
        dataErrService.updateById(dataErr);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("waterfgs:dataerr:delete")
    public R delete(@RequestBody String[] deviceIds){
        dataErrService.removeByIds(Arrays.asList(deviceIds));

        return R.ok();
    }

    @RequestMapping("/fixed")
    @RequiresPermissions("waterfgs:vdataerrfgs:list")
    public R fixed(@RequestBody String[] deviceIds){
        //根据id和date读取数据，存储到待处理列表
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String[] stringDeviceId = new String[deviceIds.length];
        //List<DataErrFixedInfoEntity> listDataErrFixedInfo = new ArrayList<>();
        for(int i = 0; i < deviceIds.length; i++){
            stringDeviceId[i] = deviceIds[i].split("_")[0];
            DataErrFgsEntity dataErrEntity = dataErrService.getOne(
                    new QueryWrapper<DataErrFgsEntity>()
                            .eq("device_id",stringDeviceId[i] )
                            .eq("now_date",deviceIds[i].split("_")[1])
            ,true);

            DataErrFixedInfoFgsEntity dataErrFixedInfoEntity = new DataErrFixedInfoFgsEntity();
            dataErrFixedInfoEntity.setOpAddDate(format.format(new Date()));

            dataErrFixedInfoEntity.setDeviceId(dataErrEntity.getDeviceId());
            dataErrFixedInfoEntity.setNowDate(dataErrEntity.getNowDate());
            dataErrFixedInfoEntity.setNowData(dataErrEntity.getNowData());
            dataErrFixedInfoEntity.setDeviceStatus(dataErrEntity.getDeviceStatus());
            dataErrFixedInfoEntity.setAgoData(dataErrEntity.getAgoData());
            dataErrFixedInfoEntity.setAgoDeviceStatus(dataErrEntity.getAgoDeviceStatus());
            dataErrFixedInfoEntity.setProName(dataErrEntity.getProName());
            dataErrFixedInfoEntity.setDelFlag(dataErrEntity.getDelFlag());
            dataErrFixedInfoEntity.setMemo1(dataErrEntity.getMemo1());
            dataErrFixedInfoEntity.setMemo2(dataErrEntity.getMemo2());
            dataErrFixedInfoEntity.setMemo3(dataErrEntity.getMemo3());

            //listDataErrFixedInfo.add(dataErrFixedInfoEntity);
            dataErrFixedInfoService.save(dataErrFixedInfoEntity);

        }

        //dataErrFixedInfoService.saveOrUpdateBatch(listDataErrFixedInfo);

        //逻辑删除历史报警记录，该id的报警都标记删除
        dataErrService.removeByIds(Arrays.asList(stringDeviceId));

        return R.ok();
    }

}
